﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using DAL;
using System.Data;
using Model;

namespace BLL
{
    public class ParticipantService
    {
        private ParticipantsDAL participantDAL = new ParticipantsDAL();
        private GroupsDAL groupDAL = new GroupsDAL();
        private GroupsService groupService = new GroupsService();
        private StudentService studnetservice = new StudentService();

       
        public void CreateParticipant(BoParticipants participant)
        {
            participantDAL.Insert(participant);
        }
        public void UpdateParticipant(BoParticipants participant)
        {
            participantDAL.Update(participant);
        }

        public void CreateGroup(BoGroups group, List<BoParticipants> participants)
        {
            groupDAL.Insert(group);
            foreach (BoParticipants p in participants)
            {
                p.PAR_GROUPS = group.GRO_ID;
                participantDAL.Insert(p);
            }       
        }

        public int UpdataSummary(BoParticipants par_id)
        {
            return participantDAL.UpdateSummary(par_id);
        }

        public int CheckStuId(BoParticipants participants)
        {
            return participantDAL.CheckStuId(participants);
        } 
        /// <summary>
        /// 添加一个报名小组 
        /// </summary>
        /// <param name="Participants">报名对象的集合</param>
        /// <returns>返回bool值</returns>
        public bool InsertGroup(List<BoParticipants> Participants, string ContestId, string groupName, string GRO_SPECIALTY)
        {
            
            int j = 0;
            BoGroups group  = new BoGroups();
            group.GRO_NAME = groupName;
            group.GRO_CONTESTS = ContestId;
            group.GRO_SPECIALTY = Convert.ToInt32(GRO_SPECIALTY);
            string PAR_GROUPS = groupService.InsertGroup(group);
            foreach (BoParticipants model in Participants)
            {
                model.PAR_ID = System.Guid.NewGuid().ToString();
                model.PAR_GROUPS = PAR_GROUPS;
                string PAR_STUDENTS = studnetservice.GetModelByCode(model.STU_CODE).STU_ID;
                model.PAR_STUDENTS = PAR_STUDENTS;
                j += participantDAL.Insert(model);
            }
            return j > 0;
        }
        public void RemoveParticipant(BoParticipants model)
        {
            participantDAL.Delete(model.PAR_ID);
        }
        /// <summary>
        /// 移除一个报名
        /// </summary>
        /// <param name="participants"></param>
        public bool RemoveParticipant(string STU_ID, string PAR_GROUPS, string PAR_CONTESTS)
        {
            if (participantDAL.DeleteByStuId(STU_ID,PAR_CONTESTS) > 0)
            {
                if (groupService.getCountByCon("GRO_ID = '" + PAR_CONTESTS + "'") > 1)
                {
                    return true;
                }
                else
                {
                    BoGroups groupModel = new BoGroups { GRO_ID = PAR_GROUPS };
                    return groupService.DeleteGroups(groupModel);
                }
            }
            else
                return false;
                
        }
        /// <summary>
        /// 获得V_Participant的视图
        /// </summary>
        /// <param name="constr"></param>
        /// <returns></returns>
        public DataTable GetV_Participant(string constr)
        {
            return participantDAL.GetV_Participant(constr);
        }
        /// <summary>
        /// 根据竞赛的id来获取V_Participant的视图
        /// </summary>
        /// <param name="ConId">竞赛id</param>
        /// <returns>返回table</returns>
        public DataTable GetV_ParticipantByConId(string ConId)
        {
            return participantDAL.GetV_ParticipantByConId(ConId);
        }

        public void RemoveGroup(BoGroups group)
        {
            participantDAL.RemoveGroupParticipants(group.GRO_ID);
            groupDAL.Delete(group.GRO_ID);
        }

        /// <summary>
        /// 根据报名id来获取一条报名数据
        /// </summary>
        /// <param name="pAR_ID"></param>
        /// <returns></returns>
        public BoParticipants getParticipant(string pAR_ID)
        {
            return participantDAL.GetModel(pAR_ID);
        }
        /// <summary>
        /// 拿到所有的报名数据
        /// </summary>
        /// <returns></returns>
        public IList<BoParticipants> getAllParticipants()
        {
            return participantDAL.GetAllParticipants();
        }
        /// <summary>
        /// 根据竞赛id以及学生学号来拿到相应的数据
        /// </summary>
        /// <param name="contestId">竞赛id</param>
        /// <param name="studentCode">学生学号</param>
        /// <returns>返回报名对象数据</returns>
        public BoParticipants GetParticipantsByCode(string studentCode, string contestId)
        {
            return participantDAL.GetModelByCode(studentCode, contestId);
        }
        /// <summary>
        /// 根据竞赛id以及学生学号获取报名列表和学生的视图
        /// </summary>
        /// <param name="contestId">竞赛id</param>
        /// <param name="studentCode">学生学号</param>
        /// <returns>返回相应的表格数据</returns>
        public DataTable GetParicipantAndStudentByCode(string studentCode, string contestId)
        {
            return participantDAL.GetParicipantAndStudentByCode(studentCode, contestId);
        }
        /// <summary>
        /// 根据小组的id获取报名列表和学生的视图
        /// </summary>
        /// <param name="GroupId">小组id</param>
        /// <returns>返回相应的表格数据</returns>
        public DataTable GetParicipantAndStudentByGrup(string GroupId)
        {
            return participantDAL.GetParicipantAndStudentByGrup(GroupId);
        }

        public IList<BoParticipants> GetSummaryParticipantsByContest(string contest)
        {
            return participantDAL.GetSummaryParticipantsByContest(contest);
        }

        public IList<BoParticipants> getAwardsPar()
        {
            return participantDAL.GetAwardsPar("-1");
        }

        public IList<BoParticipants> getAwardsPar(string contestId)
        {
            return participantDAL.GetContestAwards(contestId);
        }

        public IList<BoParticipants> getAllOutterAwardsPar()
        {
            return participantDAL.GetAllOutterAwardsPar();
        }

        public IList<BoParticipants> getAllInnerAwardsPar()
        {
            return participantDAL.GetAllInnerAwardsPar();
        }
        /// <summary>
        /// 根据竞赛对象来获取相应的小组列表
        /// </summary>
        /// <param name="contest">竞赛对象</param>
        /// <returns>返回小组列表</returns>
        public IList<BoGroups> GetContestGroups(BoContests contest)
        {
            return groupDAL.GetContestGroups(contest.CON_ID);
        }

        public IList<BoParticipants> GetContestParticipants(string  contest)
        {
            return participantDAL.GetContestParticipants(contest);
        }

        /// <summary>
        /// 通过竞赛id来获取相应的没有成绩的报名信息
        /// </summary>
        /// <param name="Con_Id"></param>
        /// <returns></returns>
        public IList<BoParticipants> GetContestHasNoScoreParticipants(string Con_Id)
        {
            return participantDAL.GetContestHasNoScoreParticipants(Con_Id);
        }
         /// <summary>
        /// 根据竞赛获取已经有成绩的报名信息
        /// </summary>
        /// <param name="contestId">竞赛id</param>
        /// <returns>返回相应的报名信息</returns>
        public IList<BoParticipants> GetContestHasScoreParticipants(string contestId)
        {
            return participantDAL.GetContestHasScoreParticipants(contestId);
        }

        public IList<BoParticipants> GetGroupParticipants(string group)
        {
            return participantDAL.GetGroupParticipants(group);
        }
        /// <summary>
        /// 根据竞赛id来插入学生的等级
        /// </summary>
        /// <param name="Con_Id">竞赛的id</param>
        /// <returns>返回bool值</returns>
        public bool innerAward(string Con_Id,bool checkgrouped)
        {
            BLL.PercentService percentservice = new PercentService();
            string constr = "PER_CONTEST ='" + Con_Id + "'";
            participantDAL.ReSetPAR_AWARDS(Con_Id);
            IList<BoPercent> PercentList = percentservice.Getv_Participant(constr);
            if (PercentList != null && PercentList.Count > 0)
                if (!checkgrouped)
                    return participantDAL.innerAward(Con_Id, PercentList) > 0;
                else
                    return participantDAL.innerGroupAward(Con_Id, PercentList) > 0;
            else
                return false;
        }

        /// <summary>
        /// 根据竞赛的id将该竞赛下的所欲的PAR_AWARDS置为-1
        /// </summary>
        /// <param name="Con_Id"></param>
        public void ReSetPAR_AWARDS(string Con_Id)
        {
            participantDAL.ReSetPAR_AWARDS(Con_Id);
        }

        /// <summary>
        /// 根据竞赛id来检测是否是小组
        /// </summary>
        /// <param name="Con_Id"></param>
        /// <returns></returns>
        public bool chekIsGroup(string Con_Id)
        {
            CompetitionService competitionservice = new CompetitionService();
            BoCompetitions CompetitionsModel = competitionservice.getCompetitionsByContest(Con_Id);
            return CompetitionsModel.COM_FORM == 1;
        }

        /// <summary>
        /// 更新具有小组的成绩
        /// </summary>
        /// <param name="ConId">竞赛id</param>
        /// <param name="Score">成绩</param>
        /// <param name="GroupId">小组id</param>
        /// <returns>返回bool值</returns>
        public bool updateGroupScore(string ConId,int Score, string GroupId)
        {
            return participantDAL.updateGroupScore(ConId,Score,GroupId) > 0;
        }
    }
}
